package com.handpay.nocardpay.baselibrary.guide.presenter;

import android.content.Context;
import android.os.StrictMode;
import android.text.TextUtils;

import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.commbase.model.IBaseModel;
import com.handpay.nocardpay.baselibrary.commbase.model.bean.BaseErrorInfo;
import com.handpay.nocardpay.baselibrary.commbase.presenter.IBasePresenter;
import com.handpay.nocardpay.baselibrary.guide.model.ICheckVersion;
import com.handpay.nocardpay.baselibrary.guide.model.VersionModelImpl;
import com.handpay.nocardpay.baselibrary.guide.model.bean.VersionBean;
import com.handpay.nocardpay.baselibrary.guide.view.IVersionDescView;
import com.handpay.nocardpay.baselibrary.guide.view.IVersionView;
import com.handpay.nocardpay.baselibrary.utils.ClientEngine;
import com.handpay.nocardpay.baselibrary.utils.NoCardPayConstant;
import com.handpay.nocardpay.baselibrary.utils.PreferenceManager;

import java.util.HashMap;

/**
 * Created by sxshi on 2017/8/9.
 */

public class VersionPresenterImpl implements IBasePresenter {
    private ICheckVersion<VersionBean> mVerIBaseModel;
    private IVersionView mVerView;
    private IVersionDescView mVersionDescView;
    private Context mContext;

    public void setmVerView(IVersionView mVerView) {
        this.mVerView = mVerView;
    }

    public void setmVersionDescView(IVersionDescView mVersionDescView) {
        this.mVersionDescView = mVersionDescView;
    }

    public VersionPresenterImpl(Context mContext) {
        this.mContext = mContext;
        this.mVerIBaseModel = new VersionModelImpl(mContext);
    }

    public VersionPresenterImpl(IVersionView versionView, Context context) {
        this.mVerIBaseModel = new VersionModelImpl(context);
        this.mVerView = versionView;
        this.mContext = context;
    }

    /**
     * 版本检测
     *
     * @param paramers      参数
     * @param isShowLoading 是否显示Loading
     */
    public void checkVersion(HashMap<String, Object> paramers, boolean isShowLoading) {
        if (isShowLoading) {
            mVerView.showProgress();
        }
        mVerIBaseModel.checkVersion(paramers, VersionBean.class, new IBaseModel.OnRequestFinishedListener<VersionBean>() {
            @Override
            public void onError(BaseErrorInfo baseErrorInfo) {
                if (mVerView != null) {
                    mVerView.hideProgress();
                    mVerView.showErrorAlert(baseErrorInfo);
                }
            }

            @Override
            public void onSuccess(VersionBean response) {
                if (mVerView != null) {
                    mVerView.hideProgress();
                    String version = response.getVersion();
                    String url = response.getUrl();
                    String description = response.getDescription();
                    String forcedUpdate = response.getForcedUpdate();
                    String pollTime = response.getPollTime();
                    ClientEngine.getInstance().saveGlobal(NoCardPayConstant.VerifyVersionOK, true);// 只用于登陆时，是否做了版本检测
                    ClientEngine.getInstance().saveGlobal(NoCardPayConstant.VERSION, version);
                    ClientEngine.getInstance().saveGlobal(NoCardPayConstant.DESCRIPTION, description);
                    ClientEngine.getInstance().saveGlobal(NoCardPayConstant.UPDATEURL, url);
                    ClientEngine.getInstance().saveGlobal(NoCardPayConstant.FORCEDUPDATE, forcedUpdate);
                    ClientEngine.getInstance().saveGlobal(NoCardPayConstant.POLL_TIME, pollTime);

                    //存储是否显示代还款按钮
                    ClientEngine.getInstance().saveGlobal(NoCardPayConstant.SHOWCRDITPAGE, response.getShowCreditPage());
                    if (TextUtils.isEmpty(url) && TextUtils.isEmpty(description)) {
                        PreferenceManager.getInstance(mContext).putBoolean(NoCardPayConstant.IS_LAST_VERSION, true);
                        mVerView.lastVersion();
                    } else {
                        PreferenceManager.getInstance(mContext).putBoolean(NoCardPayConstant.IS_LAST_VERSION, false);
                        description = description.replace("&", "\n");
                        String title = mContext.getString(R.string.updatetitle, version);
                        String message = description;
                        int result = version.compareTo(ClientEngine.getInstance().VERSION);
                        if ("0".equals(forcedUpdate) && result > 0) {
                            //普通升级
                            boolean bNoRemind = PreferenceManager.getInstance(mContext.getApplicationContext()).getBoolean(NoCardPayConstant.IS_NOT_REMIND, false);
                            String _version = PreferenceManager.getInstance(mContext.getApplicationContext()).getString(NoCardPayConstant.NOT_REMIND_VERSION, "");
                            if (bNoRemind) {
                                //当前版本已经设置过不再提醒
                                if (!TextUtils.isEmpty(_version) && version.equals(_version)) {
                                    //不再提醒状态，在登录页不更新，在版本管理页更新。
                                    mVerView.noRemindVersion(title, message, url, version);
                                } else {
                                    mVerView.normalUpdateApk(title, message, url, version);
                                }
                            } else {
                                mVerView.normalUpdateApk(title, message, url, version);
                            }
                        } else {
                            //强制升级
                            mVerView.forcedUpdateApk(title, message, url);
                        }
                    }
                }
            }
        });
    }

    /**
     * 获取版本说明
     *
     * @param paramers
     * @param isShowLoading
     */
    public void getVirsionDesc(HashMap<String, Object> paramers, boolean isShowLoading) {
        if (mVersionDescView != null) {
            mVersionDescView.showProgress();
        }
        mVerIBaseModel.getVersionDesc(paramers, VersionBean.class, new IBaseModel.OnRequestFinishedListener<VersionBean>() {
            @Override
            public void onError(BaseErrorInfo baseErrorInfo) {
                if (mVersionDescView != null) {
                    mVersionDescView.hideProgress();
                    mVersionDescView.showErrorAlert(baseErrorInfo);
                }
            }

            @Override
            public void onSuccess(VersionBean response) {
                if (mVersionDescView != null) {
                    mVersionDescView.hideProgress();
                    mVersionDescView.getVersionDesc(response.getVersion(), response.getDescription());
                }
            }
        });
    }

    @Override
    public void onDestory() {
        if (mVerView != null) {
            mVerView = null;
        }
    }
}
